using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UIManager : Singleton_Base<UIManager>
{
    public override bool isDontDestroy => false;
    public List<Panel_Base> panels;
    public Dictionary<Type, int> panel_Dictionary=new Dictionary<Type, int>();

    public Image blackMask_Image;
    public IEnumerator blackMaskIE;
    protected override void Awake()
    {
        base.Awake();
        for (int i = 0; i < panels.Count; i++)
        {
            panel_Dictionary.Add(panels[i].GetType(), i);
        }
    }
    public T GetPanel<T>() where T : Panel_Base
    {
        if (panel_Dictionary.ContainsKey(typeof(T)))
        {
            Panel_Base target_Panel = (panels[panel_Dictionary[typeof(T)]]);
            return (T)target_Panel;
        }
        return null;
    }
    public T OpenPanel<T>() where T : Panel_Base
    {
        T panel = GetPanel<T>();
        if (panel != null)
        {
            panel.gameObject.SetActive(true);
        }
        return panel;
    }
    public void SwitchBlackMaskPerformance(bool isSwitch,float time=0,System.Action finishAction=null)
    {
        Color initColor = blackMask_Image.color;
        Color targetColor = new Color(0, 0, 0, isSwitch ? 1 : 0);
        if (isSwitch)
        {
            blackMask_Image.raycastTarget = true;
        }
        if (time > 0)
        {
            IEPool_Manager.instance.KeepTimeToDo(ref blackMaskIE, 1, null, (_time) => {
                blackMask_Image.color = Color.Lerp(initColor, targetColor, 1 - _time / 1);
                return true;
            }, () => {
                if (!isSwitch)
                {
                    blackMask_Image.raycastTarget = false;
                }
                blackMask_Image.color = targetColor;
                finishAction?.Invoke();
            });
        }
        else
        {
            IEPool_Manager.instance.StopIE(blackMaskIE);
            blackMask_Image.raycastTarget = isSwitch;
            blackMask_Image.color = targetColor;
        }
    }
}
